function selectSort (array) {
    const { length } = array
    let indexMin = 0
    for (let i = 0; i < length - 1; i++) {
      indexMin = i
      for (let j = i; j < length; j++) {
        if (array[indexMin] > array[j]) {
          indexMin = j
        }
      }
      if (i != indexMin) {
        const temp = array[i]
        array[i] = array[indexMin]
        array[indexMin] = temp
      }
    }
    return array
}

const arr = selectSort([5, 4, 3, 2, 1])
console.log(arr) // [1, 2, 3, 4, 5]
